<template>
  <a-modal :width="modalWidth"
           :title="title"
           :visible.sync="visible"
           @cancel="handleClose" class="form-preview">
    <template slot="footer">
      <a-button key="back" @click="handleClose">
        关闭
      </a-button>
      <a-button key="submit" type="primary"  @click="handleOk">
        提交
      </a-button>
    </template>
      <div>
        <k-form-build
          :value="jsonData"
          :disabled="disabled"
          ref="KFB"
          @submit="handleOk"
        />
      </div>
  </a-modal>
</template>

<script>
  import {METHOD, removeAuthorization, request} from '@/utils/request'
  import {postAction, putAction} from '@/api/manage'
  import {getBpmForm,getBpmFormData} from '@/api/api'

  export default {
        name: 'FormView',
        data () {
            return {
              modalWidth:800,
              title:"表单预览",
              visible: false,
              param:{},
              tableId:"",
              bpmFormId:"",
              model: {},
              jsonData: {
              },
              disabled: false,
            }
        },
        methods: {
          view(param) {
            let that = this;
            that.visible = true;
            that.param = Object.assign({}, param);
            that.$nextTick(() => {
              that.initFormView(that.param.formId);
              that.initFormData(that.param.dataId,that.param.formId);
            });
          },
          initFormView(formId) {
            getBpmForm({
              id: formId
            }).then((res) => {
              let jsonData=res.result.html;
              this.tableId=res.result.tableId;
              this.bpmFormId=res.result.id;
              if (jsonData) {
                let jData=JSON.parse(jsonData);
                this.jsonData=jData;
              }
            })
          },
          initFormData(dataId,formId){
            let that = this;
            getBpmFormData({
              dataId: dataId,
              formId: formId
            }).then((res) => {
              console.log("formData",res)
              const oldData=res.result;
              if(oldData){
                that.model=oldData;
                this.$refs.KFB.setData(oldData)
              }
            })
          },
          handleOk(e) {
            this.$refs.KFB.getData().then(res => {
              let formData = Object.assign(this.model, res);
              console.log("formData",formData)

              this.saveForm(formData)
            }).catch(err => {
              console.log(err, '校验失败')
            })
          },

          saveForm(formData){
            var that = this;
            var dataId=that.param.dataId;
            var formId=that.bpmFormId;
            var url="/online/cgform/api/customForm/"+formId+"?tabletype=1";
            console.log(formData)
            if(dataId){
              putAction(url,formData).then((res)=>{
                if(res.success){
                  that.$emit('callback',"更新成功!");
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.handleClose();
              })
            }else {
              postAction(url,formData).then((res)=>{
                if(res.success){
                  that.$emit('callback',"保存成功!");
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.handleClose();
              })
            }
          },

          handleClose() {
            this.visible = false;
          },
        },
    }
</script>